Employee Updater: Building a Client-Server Application with Macromedia Flash Data Connection Wizard and ColdFusion
Table of Contents
Managing Changes
Now you have a rather impressive application. It displays data from two tables, and, processing updates without page refreshes, it supports user login and logout, and when a user logs in, it lets the user insert, update, and delete data.
However, there is one big limitation with the current application. It makes data changes in the local DataSets within Flash and doesn't send them back to the server; therefore, the application doesn't update the back-end databases with users' changes. Handling updates requires the use of a special component called a Resolver. The Resolver's job is to track and resolve changes; it keeps a list of what changes the user has made, and then submits them to yet another web service.
First of all, you need one more web service method. Here is the code for a really simple update method. This code snippet doesn't actually update anything. It only captures updates sent to the server and saves them to a file. I like using this update method during development as it lets me ensure that my client-side Flash works properly, independent of any back-end processing. This function dumps any updates to a file named c:\test.xml, which receives updates in XML packets. You can change the path as necessary.
For now, add the following function to employees.cfc.
<CFFUNCTION NAME="Update"
HINT="Update employee records"
ACCESS="remote"
RETURNTYPE="string">
<CFARGUMENT NAME="deltaData"
TYPE="string"
REQUIRED="true">
<CFFILE ACTION="write"
ADDNEWLINE="no"
FILE="c:\test.xml"
OUTPUT="#ARGUMENTS.deltaData#">
<CFRETURN "">
</CFFUNCTION>
You will also need to refresh the web service list in Flash by going to Window > Development Panels > Web Services. Click the refresh icon to update the web service with the new function. Note that you don't need to reregister your CFC and its functions every time you change them; you only need to do so when you have added or deleted a function from your ColdFusion component.
Once you have updated employees.cfc, follow these steps to implement a Resolver:
- Select the data form. You'll add two new components to it. These are not visual components, so you can put them anywhere. To keep things simple, add them to the bottom of the form where there is room.
-
The Resolver tracks changes and prepares the delta packet, the XML packet that contains all changes for submission back to the server. Drag an RDBMSResolver component onto the data form (Figure 25).
Figure 25. Resolver components have no UI; you can place them anywhere on the Stage.
- Open the Property inspector and name the new Resolver component instance userResolver.
- Open the Components Inspector panel; ensure that you have selected userResolver and select the Parameters tab.
- To track changes and report them correctly, you must specify the table column that is the data's primary key in the Resolver. The primary key for employee data is EMPLOYEEID. Click fieldInfo and press the magnifying glass button to display a Values dialog box.
- Click the Plus (+) button to add a field.
-
Set the fieldname value to EMPLOYEEID, and the isKey value to true (Figure 26).
Figure 26. The Resolver needs to know the name of the primary key to effectively track data changes.
- Select the Bindings tab in the Component Inspector panel.
- Click the Plus (+) button to display the Add Binding dialog box.
- Select deltaPacket and click OK.
-
Bind the deltaPacket in empDataSet to the deltaPacket in userResolver. Be sure to select the correct DataSet as there are more than one (Figure 27). Click OK.
Figure 27. The deltaPacket contains data changes until they are submitted to the server for processing
You've configured the Resolver to update a delta packet whenever a user changes the empDataSet. Next, you need to tell Flash what to do with these changes. This requires one more WebServiceConnector:
- Select the data form.
- Drag a WebServiceConnector component from the Components panel onto the Stage.
- Open the Property inspector and name the new WebServiceConnector instance userUpdateService.
- Open the Components Inspector panel and select the Parameters tab.
- In the WSDLURL field, select the URL for the employees.cfc
web service. If you're using the defaults, this is
http://localhost:8500/cfflash/employees.cfc?wsdl. In the operation field, select Update. -
The Update web service method requires that you pass a parameter to the delta packet; instead of using a binding, you pass this manually. This gives you better control when you pass it. Click the userResolver component instance on the Stage and enter the following code in the Actions panel:
// Process updates on (beforeApplyUpdates) { // Pass delta packet to the Web Service _parent.userUpdateService.params = { deltaData:this.updatePacket }; // Fire Web Service _parent.userUpdateService.trigger(); }
Now you have a Resolver to track changes and a WebServiceConnector to send those changes to ColdFusion for processing. Now you need a Save button so you can save changes. Here's what you need to do:
- Select the navLogout form.
- Drag a Button control from the Components panel onto the Stage. In the Property inspector, name the instance buttonSave. Set W=75, H=22, X=180, Y=370. Click the label field and set it to Save.
Open the Actions panel and enter the following code:
// Process save button on (click) { _root.application.data.empDataSet.applyUpdates(); }- Save your changes and test the application. Now you can log in and edit, and then view the submitted changes in the ColdFusion-generated XML file at c:\test.xml.
Before we wrap up the Flash side of things, here are some final tweaks:
-
When logging out, you should probably save any unsaved changes. Select the navLogout form and the Logout button and insert the following code into the
on (click)block:// Are there any unsaved changes? if (_root.application.data.empDataSet.changesPending()) { // Yes, save them _root.application.data.empDataSet.applyUpdates(); } -
Similarly, if a user edits data for one department, but then selects another department in the upper grid, you need to save changes. To do so, modify the code in the Actions panel for deptDataSet in the data form, changing it to the following:
// Each time there is a department change on (iteratorScrolled) { // If there are unsaved changes if (_parent.empDataSet.changesPending()) { // Save them _parent.empDataSet.applyUpdates(); } // Get employees _parent.empReceiveWebServiceConnector.trigger(); } -
When the application first loads, the lower grid does not populate or display data until the user changes the department, which means that the first department does not display until the user deselects and selects it again. To fix this, add the following code to the Actions panel for deptDataSet, which is in the data form:
// After department load get employees on (afterLoaded) { _parent.empReceiveWebServiceConnector.trigger(); }
In truth, arbitrarily writing data to the server is probably not a good thing. In both of the above examples, you should probably display an alert box asking the user whether he wants to discard unsaved changes or not, but I'll leave working that out as homework for you.